/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.sd.web;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sd.common.util.UploadUtil;
import com.jeesite.modules.sd.entity.FileCabinetRecord;
import com.jeesite.modules.sd.service.FileCabinetRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;

/**
 * 模拟本系统:80端口
 */
@Slf4j
@Controller
@RequestMapping(value = "/sd/interactingThirdParties")
public class InteractingThirdPartiesController extends BaseController {

    @Autowired
    private FileCabinetRecordService fileCabinetRecordService;

    /**
     * 接收人脸信息
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadFile")
    public JSONObject testUploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        // 保存图片到本地并得到相对路径
        String uploadPicture = UploadUtil.uploadPicture(file);
        // 返回信息
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", "true");
        jsonObject.put("msg", "上传成功");
        jsonObject.put("data", uploadPicture);
        log.info("上传成功");
        return jsonObject;
    }

    /**
     * 接收人脸结果
     *
     * @param fileCabinetRecord
     * @return
     */
    @PostMapping("/resultOfFaceRecognition")
    public JSONObject testResultOfFaceRecognition(@RequestBody FileCabinetRecord fileCabinetRecord) {

        JSONObject jsonObject = new JSONObject();
        if (Objects.nonNull(fileCabinetRecord)) {

            try {
                boolean saveResultOfFaceRecognition = fileCabinetRecordService
                        .saveResultOfFaceRecognition(fileCabinetRecord);
                if (saveResultOfFaceRecognition) {
                    // 返回信息
                    jsonObject.put("status", "true");
                    jsonObject.put("msg", "操作成功");
                    return jsonObject;
                }
            } catch (Exception e) {
                jsonObject.put("status", "false");
                jsonObject.put("msg", e.getMessage());
                return jsonObject;
            }
        }
        // 返回信息
        jsonObject.put("status", "false");
        jsonObject.put("msg", "操作失败");
        return jsonObject;
    }

    /**
     * 接收开/关门结果
     *
     * @param fileCabinetRecord
     * @return
     */
    @PostMapping("/mailBoxResult")
    public JSONObject testOpenResult(@RequestBody FileCabinetRecord fileCabinetRecord) {

        // 返回信息
        JSONObject jsonObject = new JSONObject();

        try {

            boolean saveOpenCloseResult = fileCabinetRecordService.saveOpenCloseResult(fileCabinetRecord);
            if (saveOpenCloseResult) {
                jsonObject.put("status", "true");
                jsonObject.put("msg", "操作成功");
                return jsonObject;
            }
        } catch (Exception e) {
            jsonObject.put("status", "false");
            jsonObject.put("msg", e.getMessage());
            return jsonObject;
        }
        jsonObject.put("status", "false");
        jsonObject.put("msg", "操作失败");
        return jsonObject;
    }

    /**
     * 跳转发送开门命令页面
     */
    @RequestMapping(value = "index")
    public String form(FileCabinetRecord fileCabinetRecord, Model model) {
        model.addAttribute("fileCabinetRecord", fileCabinetRecord);
        return "modules/sd/sendCabinetCommand";
    }

    /**
     * 发送开门命令
     *
     * @param fileCabinetRecord
     * @return
     */
//    @RequestMapping(value = "send")
//    @ResponseBody
//    public String testOpenMailBox(FileCabinetRecord fileCabinetRecord) {
//        String url = "http://127.0.0.1:8080/sd/simulateThirdParty/testOpenMailBox";
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("cabinetno", fileCabinetRecord.getCabinetno());
//        jsonObject.put("responseStatus", fileCabinetRecord.getResponseStatus());
//        String s = HttpUtil.executeUrlOfJson(url, jsonObject);
//        return renderResult(Global.TRUE, text("发送成功！"), s);
//    }
}